package cn.wgx.module.sys.web;

import cn.wgx.common.base.IBaseController;
import cn.wgx.common.entity.sys.Permission;
import cn.wgx.common.util.ResponseDataUtil;
import cn.wgx.module.sys.service.PermissionService;
import com.alibaba.fastjson.JSONArray;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@Api(tags = "3.0 权限管理")
@RestController
@RequestMapping("/api/per")
public class PermissionCtrl extends IBaseController<PermissionService, Permission> {

    @ApiOperation("获取全部权限列表(树形结构)")
    @GetMapping("getlist")
    public Map getList(
            @ApiParam("是否树形数据") @RequestParam(defaultValue = "false", required = false) Boolean tree,
            @ApiParam("只要menu") @RequestParam(defaultValue = "false", required = false) Boolean menu) {

        List<Permission> list;

        if (menu) {
            list = service.getMenu();
        } else {
            list = service.list();
        }
        if (tree) {
            JSONArray j = service.sortPermission(list);
            return ResponseDataUtil.returnData(j);
        }
        return ResponseDataUtil.returnData(list);
    }
}
